package com.advert.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.advert.common.core.redis.RedisCache;
import com.advert.mqtt.properties.MqttProperties;
import com.advert.service.IDeviceService;
import com.advert.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Slf4j
@Service
public class DeviceServiceImpl implements IDeviceService {


    @Autowired
    RedisCache redisCache;

    @Autowired
    RequestUtils requestUtils;

    @Autowired
    private MqttProperties mqttProperties;

    static final String DEVICE_KEYS_PREFIX = "DEVICE_KEYS:";

    static final String DEVICE_PREFIX = "DEVICESTATUS:";

    @Override
    public void handlerCabinet(JSONObject payload) {

        byte[] mqttByes = payload.getBytes("MqttBody");
        String mqttBody = new String(mqttByes);
        try {
            JSONObject jsonObject = parseJson(mqttBody);
            if (jsonObject == null) {
                return;
            }
            String event = jsonObject.getString("event");
            String uid = jsonObject.getString("uid");
            String msgId = jsonObject.getString("msgid");
            String time = jsonObject.getString("time");

            // duplication(DEVICE_KEYS_PREFIX + uid + ":" + event + "_" + msgId + "_" + time);

            if (event.equals("uploadDeviceStatus")) {

                String biz = jsonObject.getString("biz");

                log.info("###进入到uploadDeviceStatus 里###" + biz);
                JSONObject bizContent = JSON.parseObject(biz);

                String rkey = DEVICE_PREFIX + uid;

                redisCache.setCacheObject(rkey, bizContent);
                redisCache.expire(rkey, 60 * 10);

            } else {
                log.info("###事件非uploadDeviceStatus###" + event);
            }
        } catch (Exception e) {
            log.error("处理mqtt消息失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }

//    private JSONObject gMqttMessage(String event, String sk, JSONObject bizContent) {
//        Map<String, Object> params = new HashMap<>();
//
//        // JSONObject params = new JSONObject();
//        params.put("uid", "server");
//        params.put("msgid", IdUtils.fastUUID());
//        params.put("event", event);
//        params.put("time", String.valueOf(Math.round(System.currentTimeMillis() / 1000)));
//        params.put("apiv", "1.0");
//        params.put("biz", bizContent.toJSONString());
//
//
//        com.alibaba.fastjson2.JSONObject result = new com.alibaba.fastjson2.JSONObject();
//        result.put("uid", params.get("uid"));
//        result.put("msgid", params.get("msgid"));
//        result.put("event", params.get("event"));
//        result.put("time", params.get("time"));
//        result.put("apiv", params.get("apiv"));
//        result.put("biz", params.get("biz"));
//
//        try {
//            String sign = SignUtils.generateSignature(params, sk);
//            result.put("sign", sign);
//        } catch (Exception e) {
//
//        }
//
//        return null;
//
//
//    }

    JSONObject parseJson(String payload) {
        try {
            return JSONObject.parseObject(payload);
        } catch (Exception e) {
            log.error("解析json报错", payload);
            return null;
        }
    }

    public void duplication(String key) throws Exception {
        if (redisCache.get(key) != null) {
            throw new Exception("msgID重复,key: " + key);
        }
        redisCache.set(key, 0);
        // 5分钟后过期
        redisCache.expire(key, 180);
    }
}
